package com.zzu.team.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzu.team.entity.Question;
import com.zzu.team.entity.ReturnBean;
import com.zzu.team.service.QuestionService;
import org.springframework.data.relational.core.sql.In;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 问题表(Question)表控制层
 *
 * @author makejava
 * @since 2021-11-27 19:42:24
 */
@RestController
@RequestMapping("question")
public class QuestionController extends BaseController {

    @Resource
    QuestionService questionService;

    @GetMapping("selectAll")
    public ReturnBean<List<Question>> selectAll(Integer page, Integer limit, Question question) {
        ReturnBean<List<Question>> returnBean = new ReturnBean<>();
        returnBean.setCode(0);
        returnBean.setMsg("查询");

        Page<Question> questionPage = new Page<>(page-1, limit);

        Page<Question> ques = questionService.page(questionPage, new QueryWrapper<>(question));

        returnBean.setData(ques.getRecords());
        returnBean.setCount(ques.getTotal());

        return returnBean;
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ReturnBean selectOne(@PathVariable Serializable id) {
        Question question = questionService.getById(id);
        return success(question);
    }

    /**
     * 根据type查询
     */
    @GetMapping("selectByType")
        public ReturnBean selectByType(@RequestParam Integer type){
        Map<String, Object> map = new HashMap<>();
        map.put("type", type);
        List<Question> questions = questionService.listByMap(map);
        if (questions.size()>0) return success(questions);
        else return fail(questions);
    }


    /**
     * 插入数据
     * @param question
     * @return
     */
    @PostMapping("insert")
    public ReturnBean insert(@RequestBody Question question) {

        boolean save = questionService.save(question);
        System.out.println("save:"+save);
        if (save) return super.success(question);

        return super.fail(question);
    }


    /**
     * 修改数据
     *
     * @param question 实体对象
     * @return 修改结果
     */
    @PutMapping("update")
    public ReturnBean update(@RequestBody Question question) {
        boolean update = questionService.updateById(question);
        if (update) return super.success(question);
        else return super.fail(question);
    }

    /**
     * 删除数据
     * 根据问题id批量删除问题
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping("deleteByIdList")
    public ReturnBean delete(@RequestParam("idList") List<Long> idList) {
        boolean delete = questionService.removeByIds(idList);
        if (delete) return success(idList);
        else return fail(idList);
    }

    /**
     * 根据问题id删除一个问题
     * @param id
     * @return
     */
    @DeleteMapping("deleteById")
    public ReturnBean delete(@RequestParam("id")Integer id) {
        boolean delete = questionService.removeById(id);
        if (delete) return success(questionService.getById(id));
        return fail(questionService.getById(id));
    }
}

